package com.putao.album.guide;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.nineoldandroids.animation.ObjectAnimator;
import com.putao.album.R;
import com.putao.album.application.GlobalApplication;
import com.putao.album.base.BaseFragment;
import com.putao.album.guide.anim.AnimElement;
import com.putao.album.guide.anim.ConfigInfo;
import com.putao.album.guide.anim.GuideAnmiUtil;
import com.putao.album.main.AlbumHomeActivity;
import com.putao.album.user.ActivityLoginRegister;
import com.putao.album.util.ActivityHelper;
import com.putao.album.util.DisplayHelper;
import com.putao.album.util.Loger;
import com.putao.album.util.PuTaoConstants;
import com.putao.album.util.SharedPreferencesHelper;
import com.putao.album.util.StringHelper;
import com.putao.album.util.ViewHelper;

import java.util.ArrayList;

/**
 * Created by jidongdong on 15/4/23.
 */
public class GuideFragment extends BaseFragment implements IGuideAnimation {
    private Button btn_login;
    private int page_index;
    private boolean showbtn = false;
    private boolean newver = false;
    private FrameLayout page_container;
    private ArrayList<AnimElement> elements = new ArrayList<AnimElement>();
    private boolean flag_palyed = false;

    @Override
    public int doGetContentViewId() {
        return R.layout.fragment_guide_layout;
    }

    @Override
    public void doInitSubViews(View view) {
        btn_login = queryViewById(R.id.btn_login);
        page_container = queryViewById(R.id.page_container);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String current_user = SharedPreferencesHelper.readStringValue(getActivity(), PuTaoConstants.PREFERENCE_KEY_MOBILE, "");
                ActivityHelper.startActivity(getActivity(), (newver && !StringHelper.isEmpty(current_user)) ? AlbumHomeActivity.class : ActivityLoginRegister.class);
                getActivity().finish();
            }
        });
    }

    @Override
    public void doInitDataes() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            showbtn = bundle.getBoolean("showbtn");
            newver = bundle.getBoolean("newver");
            page_index = bundle.getInt("index");

            initUILayout();
        }
    }

    /**
     * 初始化界面元素
     */
    void initUILayout() {
        int layout = 0;
        switch (page_index) {
            case 0:
                layout = R.layout.guide_layout_1;
                break;
            case 1:
                layout = R.layout.guide_layout_2;
                break;
            case 2:
                layout = R.layout.guide_layout_3;
                break;
        }
        page_container.addView(LayoutInflater.from(mActivity).inflate(layout, null));
        setAnimElements();
    }

    /**
     * 设置当前fragment动画元素
     */
    void setAnimElements() {
        ArrayList<ConfigInfo.Elements> config_element = GuideAnmiUtil.getSenceElements(page_index);
        if (config_element == null) {
            Loger.d("animate elements is null");
            return;
        }
        for (int i = 0; i < config_element.size(); i++) {
            ConfigInfo.Elements ele = config_element.get(i);
            float start_x = ele.getStart_pos_x() / 2 * DisplayHelper.getDensity();
            float start_y = ele.getStart_pos_y() / 2 * DisplayHelper.getDensity();
            AnimElement item = new AnimElement();
            item.ratio = ele.getRatio();
            item.startX = start_x;
            item.startY = start_y;
            item.element = getElementViewById(ele.getViewid(), start_x, start_y);
            elements.add(item);
        }
    }

    /**
     * 设置动画元素布局
     *
     * @param id
     * @param x
     * @param y
     * @return
     */
    View getElementViewById(String id, float x, float y) {
        if (StringHelper.isEmpty(id)) {
            return null;
        }
        int vid = getResources().getIdentifier(id, "id", GlobalApplication.getInstance().getPackageName());
        int rid = getResources().getIdentifier(id, "drawable", GlobalApplication.getInstance().getPackageName());
        Bitmap bitmap = null;
        if (rid > 0) {
            bitmap = ((BitmapDrawable) getResources().getDrawable(rid)).getBitmap();
        }
        View view = ViewHelper.queryViewById(page_container, vid);
        if (view != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            if (bitmap != null) {
                params.width = bitmap.getWidth();
                params.height = bitmap.getHeight();
            }
            params.leftMargin = (int) x;
            params.topMargin = (int) y;
            view.setLayoutParams(params);
        } else {
            Loger.d("view elements can not find");
        }
        return view;
    }


    @Override
    public void Play() {
        if (showbtn && !flag_palyed) {
            btn_login.setAlpha(0f);
            btn_login.setText(newver ? R.string.using_new_version : R.string.start_using_app);
            ObjectAnimator.ofFloat(btn_login, "alpha", 0f, 1f).setDuration(500).start();
            flag_palyed = true;
        }
    }

    @Override
    public void Move(int distance) {
        for (int i = 0; i < elements.size(); i++) {
            AnimElement ele = elements.get(i);
            View v = ele.element;
            if (v != null) {
                float ratio = ele.ratio;
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) elements.get(i).element.getLayoutParams();
                params.leftMargin = (int) (ele.startX - distance * ratio + 0.5f);
                elements.get(i).element.setLayoutParams(params);
            }
        }
    }
}
